package com.qianfeng.controller;

import com.qianfeng.dto.Message;
import com.qianfeng.dto.Page;
import com.qianfeng.dto.Result;
import com.qianfeng.entity.Student;
import com.qianfeng.enums.CommonEnum;
import com.qianfeng.exceptions.CommonException;
import com.qianfeng.service.StudentService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequiresRoles(value = "teacher")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 分页
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequiresRoles(value = "teacher")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/getStudentPage")
    public Result getStudentPage(Integer pageNo, Integer pageSize) {
        Result result = new Result();
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 2;
        }
        Page studentPage = studentService.getStudentPage(pageNo, pageSize);
        System.out.println(studentPage);
        result.setObj(studentPage);
        result.setMessage(new Message(200, "成功"));
        return result;
    }

    /**
     * 添加
     *
     * @param
     * @return
     */
    @RequiresRoles(value = "teacher")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
    public Result addStudent(Student student) {
        System.out.println("添加Student" + student);
        try {
            Result result = new Result();
            studentService.addStudent(student);
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.ADD_STUDENT_ERROR);
        }
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    @RequiresRoles(value = "teacher")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/deleteStudent")
    public Result deleteStudent(Integer studentId) {
        try {
            studentService.deleteStudent(studentId);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.DELETE_STUDENT_ERROR);
        }
    }

    /**
     * 更新
     *
     * @param
     * @return
     */
    @RequiresRoles(value = "teacher")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/updateStudent", method = RequestMethod.POST)
    public Result updateStudent(Student student) {
        try {
            studentService.updateStudent(student);
            Result result = new Result();
            Message message = new Message();
            result.setMessage(message);
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.UPDATE_STUDENT_ERROR);
        }
    }
    @RequiresRoles(value = "teacher")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/getStudentByStuId")
    public Result getStudentByStuId(Integer studentId) {
        Result result = new Result();
        Student studentByStudentId = studentService.getStudentByStudentId(studentId);
        result.setMessage(new Message());
        result.setObj(studentByStudentId);
        return result;
    }

}
